package com.dlc.shop.combo.api.listener;

import cn.hutool.core.collection.CollUtil;
import com.dlc.shop.bean.app.dto.ShopCartItemDto;
import com.dlc.shop.bean.app.vo.ProductVO;
import com.dlc.shop.bean.event.LoadOrderItemGiveawayEvent;
import com.dlc.shop.bean.event.LoadProdActivistEvent;
import com.dlc.shop.bean.order.LoadProdActivistOrder;
import com.dlc.shop.bean.vo.ComboVO;
import com.dlc.shop.bean.vo.GiveawayVO;
import com.dlc.shop.combo.multishop.service.ComboService;
import com.dlc.shop.combo.multishop.service.GiveawayService;
import lombok.AllArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * 加载商品套餐信息
 * @author yami
 */
@Component("comboLoadProdActivistListener")
@AllArgsConstructor
public class LoadProdActivistListener {

    private final ComboService comboService;
    private final GiveawayService giveawayService;

    @EventListener(LoadProdActivistEvent.class)
    @Order(LoadProdActivistOrder.COMBO)
    public void loadProdComboHandle(LoadProdActivistEvent event) {
        ProductVO productVO = event.getProductVO();
        // 秒杀活动页面，不显示套餐
        if (Objects.nonNull(productVO.getSeckillVO())) {
            return;
        }
        List<ComboVO> comboList = comboService.listComboByProdId(productVO.getProdId());
        productVO.setComboList(comboList);

        GiveawayVO giveawayVO = giveawayService.getGiveawayProdByProdId(productVO.getProdId());
        productVO.setGiveaway(giveawayVO);
    }

    @EventListener(LoadOrderItemGiveawayEvent.class)
    public void loadProdComboHandle(LoadOrderItemGiveawayEvent event) {
        List<ShopCartItemDto> shopCartItems = event.getShopCartItems();
        // 秒杀活动页面，不显示套餐
        if (CollUtil.isEmpty(shopCartItems)) {
            return;
        }
        for (ShopCartItemDto shopCartItem : shopCartItems) {
            GiveawayVO giveawayVO = giveawayService.getGiveawayProdByProdId(shopCartItem.getProdId());
            shopCartItem.setGiveaway(giveawayVO);
        }
    }

}
